<?
 /*****************************************************************************
 * contacts.php                                                               *
 * Last Modified: 2007-06-06                                                  *
 *                                                                            *
 * contacts.php is used to output a list of the contacts in the database with *
 * a few options (all <default>, search lastName, search company). This file  *
 * also allows administrators to add, delete or edit records. Users are only  *
 * allowed to view records; they may not add, delete or edit records.         *
 * All users may use the search feature.                                      *
 *                                                                            *
 * BBG_Billing, a PHP application using MySQL for creating and maintaining a  *
 * contacts and invoices database.                                            *
 *                                                                            *
 * @copyright Copyright (C) 2007, Bugs Bee Gone Computer Services             *
 * @owner     Daniel Barnett (Bugs Bee Gone Computer Services)                *
 * @author    Daniel Barnett <dbarnett@bugsbeegone.com>                       *
 * @website   http://www.bugsbeegone.com                                      *
 * @license   http://www.gnu.org/licenses/gpl.html GNU General Public License *
 * @package   BBG_Billing                                                     *
 * @name      contacts.php                                                    *
 * @version   1.0.0                                                           *
 * @uses      Jpmaster77's Login Script (Written by: Jpmaster77, 2004-08-19)  *
 *             ->(http://www.evolt.org/PHP-Login-System-with-Admin-Features/) *
 *                                                                            *
 * This file is part of the "BBG_Billing" PHP application.                    *
 *                                                                            *
 * BBG_Billing is free software; you can redistribute it and/or modify        *
 * it under the terms of the GNU General Public License as published by       *
 * the Free Software Foundation; either version 2 of the License, or          *
 * (at your option) any later version.                                        *
 *                                                                            *
 * BBG_Billing is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU General Public License for more details.                               *
 *                                                                            *
 * You should have received a copy of the GNU General Public License          *
 * along with BBG_Billing; if not, write to the Free Software                 *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
 *****************************************************************************/
 
  // Require session.php for user access control
	require "include/session.php";

  // If user is not logged in, redirect to main.php
	if(!$session->logged_in){
		header("Location: main.php");
		exit();
	}

  // Require config.inc.php for configuration variables
	require "config.inc.php";

  // Connect to database
	mysql_connect($path, $username, $password);
	// Select the database, if error die displaying error message
	@mysql_select_db($database) or die(mysql_error());

  // Get searchType value from URL
	$searchType=$_GET['searchType'];
	// Get lastName value from URL
	$lastName=$_GET['lastName'];
	// Get company value from URL
	$company=$_GET['company'];
	
	// DETERMINE WHICH SEARCH TYPE TO USE
	// If searchType = "lastName"...
	if($searchType=="lastName") {
    // Display only records WHERE Last Name LIKE $lastName
		$query="SELECT * FROM tblContacts WHERE lastName LIKE '%$lastName%'" . "ORDER BY lastName, firstName, customerID ASC";
		$searchString='Search Results for: "' . $lastName . '"';
  // If searchType = "company"...
	} else if($searchType=="company") {
    // Display only records WHERE company LIKE $company
		$query="SELECT * FROM tblContacts WHERE company LIKE '%$company%'" . "ORDER BY company, lastName, firstName, customerID ASC";
		$searchString='Search Results for: "' . $company . '"';
  // If searchType != "lastName" OR "company"...
	} else {
    // Display all records
		$query="SELECT * FROM tblContacts ORDER BY lastName, firstName, customerID ASC";
		$searchString="~ All Records ~";
	}

  // Execute the MySQL query
	$result = mysql_query($query);

  // Determine number of rows returned
	$num = mysql_numrows($result);

  // Close the database connection
	mysql_close();

echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n";

echo '<html xmlns="http://www.w3.org/1999/xhtml">' . "\n";
	echo '<head>' . "\n";
		echo '<meta name="ROBOTS" content="NOODP, NOFOLLOW" />' . "\n";
		echo '<meta name="GOOGLEBOT" content="NOODP, NOFOLLOW" />' . "\n";
		echo '<title>Bugs Bee Gone - Billing - Contacts List</title>' . "\n";
		echo '<meta name="description" content="Bugs Bee Gone - Billing - Contacts List" />' . "\n";
		echo '<meta name="keywords" content="php, mysql, test, practice" />' . "\n";
		echo '<link rel="shortcut icon" href="" />' . "\n";
		echo '<link href="contact.css" type="text/css" rel="stylesheet" />' . "\n";

		echo '<script type="text/javascript">' . "\n";
			echo 'function clearSearch() {' . "\n";
				echo 'window.location="contacts.php";' . "\n";
			echo '}' . "\n";

			echo 'function searchLastName() {' . "\n";
				echo 'document.search.lastName.disabled=false;' . "\n";
				echo 'document.search.company.disabled=true;' . "\n";
			echo '}' . "\n";

			echo 'function searchCompany() {' . "\n";
				echo 'document.search.company.disabled=false;' . "\n";
				echo 'document.search.lastName.disabled=true;' . "\n";
			echo '}' . "\n";
		echo '</script>' . "\n";
	echo '</head>' . "\n";

	echo '<body onload="searchLastName()">' . "\n";
	echo '<p>Bugs Bee Gone</p>' . "\n";
	echo '<p style="font-weight:normal;"><a href="billing.php">Billing</a>' . "\n";
	echo ' -> Contacts List<br />&nbsp;<br />' . "\n";
	
	// Output the value of searchString
	echo $searchString . '</p>' . "\n";

  // Determine if any rows were returned by the MySQL query executed on line 80
  // If no rows were returned...
	if($num == 0) {
		echo '<br /><hr />' . "\n";
		echo '<br /><p style="color:red; text-align:center;">No Records Found</p>';
  // If rows were returned, output the rows in a table
	} else {
		echo '<table width="100%" border="0" cellspacing="0" cellpadding="2" align="center">' . "\n";
		echo '<tr>' . "\n";
			echo '<th><font face="Arial, Helvetica, sans-serif">View</font></th>' . "\n";
			echo '<th><font face="Arial, Helvetica, sans-serif">Company</font></th>' . "\n";
			echo '<th><font face="Arial, Helvetica, sans-serif">Name</font></th>' . "\n";
			echo '<th><font face="Arial, Helvetica, sans-serif">Phone</font></th>' . "\n";
			echo '<th><font face="Arial, Helvetica, sans-serif">Phone</font></th>' . "\n";
			echo '<th><font face="Arial, Helvetica, sans-serif">E-Mail</font></th>' . "\n";
			echo '<th><font face="Arial, Helvetica, sans-serif">Edit</font></th>' . "\n";
			echo '<th><font face="Arial, Helvetica, sans-serif">Delete</font></th>' . "\n";
		echo '</tr>' . "\n";

    // Output the rows returned from the MySQL query executed on line 80
		$i = 0;
		while ($i < $num)
		{
			$key = mysql_result($result,$i,"key");
			$customerID = mysql_result($result,$i,"customerID");
			$company = mysql_result($result,$i,"company");
			$firstName = mysql_result($result,$i,"firstName");
			$lastName = mysql_result($result,$i,"lastName");
			$address1 = mysql_result($result,$i,"address1");
			$address2 = mysql_result($result,$i,"address2");
			$city = mysql_result($result,$i,"city");
			$state = mysql_result($result,$i,"state");
			$zipCode = mysql_result($result,$i,"zipCode");
			$phone1 = mysql_result($result,$i,"phone1");
			$phone2 = mysql_result($result,$i,"phone2");
			$fax = mysql_result($result,$i,"fax");
			$email = mysql_result($result,$i,"email");
			$webSite = mysql_result($result,$i,"webSite");
			$notes = mysql_result($result,$i,"notes");

		echo '<tr>' . "\n";
			echo '<td class="border"><font face="Arial, Helvetica, sans-serif"><a href="viewContact.php?key=' . $key . '">' . $customerID . '</a></font></td>' . "\n";
			
			// If $company is NULL, output a space
			if($company == "") {
				echo '<td class="border">&nbsp;</td>' . "\n";
      // If $company is not NULL, output value of company
			} else {
				echo '<td class="border"><font face="Arial, Helvetica, sans-serif">' . $company . '</font></td>' . "\n";
			}
			
			echo '<td class="border"><font face="Arial, Helvetica, sans-serif">' . $firstName . " " . $lastName . '</font></td>' . "\n";
			
			// If $phone1 is NULL, output a space
			if($phone1 == "") {
				echo '<td class="border">&nbsp;</td>' . "\n";
      // If $phone1 is not NULL, output value of phone1
			} else {
				echo '<td class="border"><font face="Arial, Helvetica, sans-serif">' . $phone1 . '</font></td>' . "\n";
			}
			
			// If $phone2 is NULL, output a space
			if($phone2 == "") {
				echo '<td class="border">&nbsp;</td>' . "\n";
      // If $phone2 is not NULL, output value of phone2
			} else {
				echo '<td class="border"><font face="Arial, Helvetica, sans-serif">' . $phone2 . '</font></td>' . "\n";
			}
			
			echo '<td class="border"><font face="Arial, Helvetica, sans-serif"><a href="mailto:' . $email . '">E-Mail</a></font></td>' . "\n";
			
			// If user is an administrator, allow them to edit or delete the contact
			if($session->isAdmin()){
				echo '<td class="border"><font face="Arial, Helvetica, sans-serif"><a href="editContact.php?key=' . $key . '">Edit</a></font></td>' . "\n";
				echo '<td class="border"><font face="Arial, Helvetica, sans-serif"><a href="confirmDeleteContact.php?key=' . $key . '">Delete</a></font></td>' . "\n";
			} else {
				echo '<td class="border">&nbsp;</td>' . "\n";
				echo '<td class="border">&nbsp;</td>' . "\n";
			}
		echo '</tr>' . "\n";

		$i++;
		}

		echo '</table>' . "\n";
	}

		echo '<br /><hr />' . "\n";

    // Search box with selection options for searching by lastName or company
		echo '<div style="text-align: center;">' . "\n";
		echo '<p>Search Contacts:</p>' . "\n";
		echo '<form action="contacts.php" method="get" name="search">' . "\n";
			echo '<table border="0" width="275px">' . "\n";
			echo '<tr>' . "\n";
				echo '<td width="50%">' . "\n";
					echo '<input type="radio" name="searchType" value="lastName" onfocus="searchLastName()" checked="checked">Last Name:<br />' . "\n";
					echo '<input type="radio" name="searchType" value="company" onfocus="searchCompany()">Company:' . "\n";
				echo '</td>' . "\n";

				echo '<td width="50%">' . "\n";
					echo '<input type="text" name="lastName" onclick="searchLastName()"><br />' . "\n";
					echo '<input type="text" name="company" onclick="searchCompany()">' . "\n";
				echo '</td>' . "\n";
			echo '</tr>' . "\n";

			echo '<tr>' . "\n";
				echo '<td colspan="2" width="100%" style="text-align:center;">' . "\n";
					echo '<input type="Submit" value="Search" name="search" /> <input type="button" onclick="clearSearch()" value="Clear Search" name="clear" />' . "\n";
				echo '</td>' . "\n";
			echo '</tr>' . "\n";
			echo '</table>' . "\n";
		echo '</form>' . "\n";
		echo '</div>' . "\n";
		// End search box

	echo '<hr />' . "\n";

  // Determine if user is an administrator, if they are allow them to create a new contact
	if($session->isAdmin()){
		echo '<p style="text-align:center;"><a href="newContact.php">Create New Contact</a></p>' . "\n";
	}

echo '</body>' . "\n";
echo '</html>' . "\n";
?>
